package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import entity.Properties;
import entity.PsLineInfo;
import entity.PsSystemconfig;
import entity.PsSystemconfig;

@Repository("configDao")
public class ConfigDao<K, V> extends HibernateDaoSupport{
	public List<PsSystemconfig> selectAllConfig(int page,int rows,DetachedCriteria searchDc){
		return (List<PsSystemconfig>)this.getHibernateTemplate().findByCriteria(searchDc, (page-1)*rows, rows);
	}
	public int count(DetachedCriteria searchDc){
		searchDc.setProjection(Projections.rowCount());
		List<Long> count=(List<Long>)this.getHibernateTemplate().findByCriteria(searchDc);
		return count.get(0).intValue();
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void changeIsStart(PsSystemconfig psConfig) throws Exception{
		PsSystemconfig p=this.getHibernateTemplate().get(PsSystemconfig.class, psConfig.getId());	
		if(p.getIsstart()==1){
			p.setIsstart((byte) -1);
		}else{
			p.setIsstart((byte) 1);
		}		
		
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteConfig(final PsSystemconfig psConfig)  throws Exception{
		this.getHibernateTemplate().delete(psConfig);		
		
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void addUpdateConfig(PsSystemconfig psConfig)  throws Exception{
		if(psConfig.getId()!=null){
			PsSystemconfig p=this.getHibernateTemplate().get(PsSystemconfig.class, psConfig.getId());
			BeanUtils.copyProperties(psConfig, p);
			
		}else{
			this.getHibernateTemplate().save(psConfig);
		}
		
		
	}
	public PsSystemconfig findConfig(PsSystemconfig psConfig){
		return ((List<PsSystemconfig>)this.getHibernateTemplate().find("from PsSystemconfig where id=?",
				psConfig.getId())).get(0);
		
	}
	public boolean findConfigValueName(PsSystemconfig psConfig){
		return ((List<PsSystemconfig>)this.getHibernateTemplate().find("from PsSystemconfig where configvaluename=?",
				psConfig.getConfigvaluename())).size()>0?true:false;
	}
	public List<Properties<K, V>> findDistinctConfigValueName(){
		List<Properties<K, V>> list=new ArrayList<Properties<K, V>>();
		List l=this.getHibernateTemplate().find("select distinct id,configvaluename from PsSystemconfig");
		for (Object o : l) {		
			Object[] temp=(Object[])o;
			list.add(new Properties(temp[0],temp[1]));
		}
		return list;
	}
	public List<Properties<K, V>> findDistinctConfigValueId(){
		List<Properties<K, V>> list=new ArrayList<Properties<K, V>>();
		List l=this.getHibernateTemplate().find("select distinct id,configvalueid from PsSystemconfig");
		for (Object o : l) {		
			Object[] temp=(Object[])o;
			list.add(new Properties(temp[0],temp[1]));
		}
		return list;
	}
}
